package com.lhkj.ct.admin.modules.gauge.qnr.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.enums.DeleteStatus;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.meta.modules.gauge.common.enums.GaugeType;
import com.lhkj.ct.meta.modules.gauge.model.dto.QuestionsDto;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblQnr;
import com.lhkj.ct.meta.modules.gauge.model.vo.QnrPageVo;
import com.lhkj.ct.meta.modules.gauge.service.GaugeQuestionService;
import com.lhkj.ct.meta.modules.gauge.service.QnrConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *      调查问卷配置
 * </p>
 *
 * @author jobob
 * @since 2023-10-26
 */
@RestController
@RequestMapping("api/plat/gauge/qnr")
@Api(tags = {"6.3调查问卷配置"})
@Validated
public class QnrConfigController {

    @Resource
    private QnrConfigService qnrConfigService;

    @Resource
    private GaugeQuestionService questionService;

    @GetMapping(value = "list")
    @ApiOperation(value = "1.列表接口")
    public ActionResult<IPage<QnrPageVo>> list(TblQnr entity, PageQuery pageQuery) {
        return ActionResult.success(qnrConfigService.selectQnrPage(entity, pageQuery));
    }

    @GetMapping(value = "{id}/get")
    @ApiOperation(value = "2.问卷详情")
    public ActionResult<TblQnr> get(@PathVariable String id) {
        TblQnr form = qnrConfigService.getForm(id);
        Validate.notNull(form, "问卷不存在或已删除！");
        form.setQuestions(questionService.selectQutWithOptWithLogicByGaugeId(form.getId(), GaugeType.QUESTIONNAIRE));
        return ActionResult.success(form);
    }

    @PostMapping(value = "insert")
    @ApiOperation(value = "3.新增")
    @Log(title = "问卷配置", actionType = ActionType.INSERT)
    public ActionResult<String> insert(@RequestBody TblQnr entity) {
        entity.setStatus(0);
        entity.setVNum(1);
        qnrConfigService.insert(entity);
        return ActionResult.success("操作成功", entity.getId());
    }

    @PutMapping(value = "update")
    @ApiOperation(value = "4.编辑")
    @Log(title = "问卷配置", actionType = ActionType.UPDATE)
    public ActionResult<String> update(@RequestBody TblQnr entity) {
        TblQnr target = qnrConfigService.getBaseMapper().selectById(entity.getId());
        Validate.isFalse(null == target || DeleteStatus.DELETED.equals(target.getDelFlag()), "问卷不存在或已删除！");
        Validate.validState(target.getStatus() != 2, "问卷已分配，不支持修改题目！");
        entity.setStatus(target.getStatus());
        qnrConfigService.updateById(entity);
        return ActionResult.success();
    }

    @PostMapping(value = "{qnrId}/saveQuestion")
    @ApiOperation(value = "5.保存题目配置")
    @Log(title = "问卷题目配置", actionType = ActionType.INSERT)
    public ActionResult<String> saveQuestion(@PathVariable String qnrId,@Validated @RequestBody QuestionsDto questionsDto) {
        questionService.saveByGauge(qnrId, GaugeType.QUESTIONNAIRE, questionsDto);
        return ActionResult.success();
    }

    @PutMapping(value = "updateStatus")
    @ApiOperation(value = "6.修改问卷已完成", notes = "支持批量")
    @Log(title = "问卷配置", actionType = ActionType.UPDATE)
    public ActionResult<Void> updateStatus(@NotEmpty(message = "删除的id不能为空") @RequestBody List<String> ids) {
        return ActionResult.judge(qnrConfigService.updateStatus(1, ids));
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "7.删除问卷", notes = "支持批量删除")
    @Log(title = "问卷配置", actionType = ActionType.DELETE)
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "删除的编号不能为空") List<String> ids) {
        int count = qnrConfigService.removeByIds(ids);
        return ActionResult.success(String.format("操作成功，共删除[%d]个问卷",count));
    }

    @PostMapping(value = "{qnrId}/copy")
    @ApiOperation(value = "8.复制问卷")
    @Log(title = "问卷配置", actionType = ActionType.INSERT)
    public ActionResult<String> copy(@PathVariable String qnrId) {
        qnrConfigService.copy(qnrId);
        return ActionResult.success();
    }
}
